<template>
    <div class="head-container">
        <a-input-search v-model:value="searchValue" placeholder="请输入井名称" style="width: 85%; margin-bottom: 10px;" @search="onSearch" />
    </div>

    <div class="head-container">
        <el-tree :data="data" :props="{ label: 'deptName', children: 'children' }" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="deptTreeRef" highlight-current :default-expand-all="false"
            @node-click="handleNodeClick" />
    </div>
</template>
<script setup>
import useDeptStore from '../../store/dept'
import useApp from '@/hooks'
const deptStore = useDeptStore()
const { proxy } = useApp();
const deptName = ref('')
const searchValue = ref('')
const data = ref([])
const emit = defineEmits(['confirm', 'search'])
const props = defineProps({
    value: {
        type: [String, Number]
    }
})
/** 节点单击事件 */
function handleNodeClick(data) {
    emit('confirm', data.id)
    // queryParams.value.deptId = data.id;
    // handleQuery();
}
/** 通过条件过滤节点  */
const filterNode = (value, data) => {
    if (!value) return true;
    //return data.label.indexOf(value) !== -1;
    return data.deptName.indexOf(value) !== -1;
};
/** 根据名称筛选部门树 */
watch(deptName, (val) => {
    proxy.$refs["deptTreeRef"].filter(val);
});
// 获取树
deptStore.getTree().then(res => {
    data.value = res
})
// 搜索
function onSearch(e) {
    // console.log('e',e);
    emit('search', e)

    /* // 过滤节点
    proxy.$refs["deptTreeRef"].filter(e);
    // 如果有匹配的节点
    const matchedNode = findMatchingNode(data.value, e);
    console.log('matchedNode',matchedNode);
    if (matchedNode) {
        // 展开匹配的节点
        proxy.$refs["deptTreeRef"].setCurrentKey(matchedNode.id);

        // 滚动到匹配的节点
        // 请确保树控件支持滚动到节点的方法
        proxy.$refs["deptTreeRef"].scrollToNode(matchedNode);
    } */
}
/* function findMatchingNode(nodes, searchTerm) {
    console.log(nodes, searchTerm);
    // 这是一个递归函数，用于查找匹配的节点
    for (const node of nodes) {
        if (node.deptName.includes(searchTerm)) {
            return node;
        } else if (node.children && node.children.length > 0) {
            const matchedChild = findMatchingNode(node.children, searchTerm);
            if (matchedChild) {
                return matchedChild;
            }
        }
    }
    return null;
} */
</script>
<style></style>